
const db = require('../config/db')

const request = require('request')

const dayjs = require('dayjs')

/**
 * 密码登录的接口逻辑
 * @param {*} req 
 * @param {*} resp 
 */
 exports.pwdLoginController = (req, resp) => {
     console.log("****登录接口****")
     console.log(req.query);
    //  let { username, password } = req.query
     const data = req.query
    // const userSelectSql = 'select * from manager where username = ? or password = ?'
     if (data.username == 'admin' && data.password == 'admin') {
         return resp.send({
             code: 0,
             message : '登录成功'
         })
     } else {
         console.log('失败');
    }
}


/**
 * 获取用户数据
 */
let List = []
exports.getUserState = (req, resp) => {
    console.log('**********获取用户数据**********');
    const page = req.query.page || 1   
    let sqlUser = 'select * from user'    
    db(sqlUser, null, data => {
        let len = data.length;
        console.log(len);
        for (let i = 0; i < data.length; i++){
            console.log('**********获取每一项**********');            
            List.push(data[i])
            console.log('获取list');           
        }      
        db(sqlUser, null, result => {
            console.log('result************************');
            // console.log(result);
            if (result.length > 0) {
                resp.send({
                    code: 0,
                    data: {
                        status: 200,
                        result
                    }                   
                })
                // console.log('成功获取用户信息',data);
            } else {
                resp.send({
                    status: 500,
                    msg : '暂无数据'
                })
            }
        })
    })
}

/***
 * 获取电动车品牌数据
 */
exports.getElecBrand = (req, resp) => {
    console.log('*****************获取电动车品牌数据 *******************');
    let sqlElec = 'select * from brand'
    db(sqlElec, null, data => {
        let len = data.length;
        console.log('************');
        console.log('len',len);
        // const sql = 'select * from user by id desc limit 8 offset' + (page - 1) * 8
        db(sqlElec, null, result => {
            console.log('result************************');
            // console.log(result);
            if (result.length > 0) {
                data.forEach(item => {
                    console.log('********获取每一项******');
                    console.log(item);
                    console.log(item.brand);
                    console.log('******** 获取logo');
                    // console.log(item.logo);
                    console.log('*********判断是否为logo******');
                    // console.log(Buffer.isBuffer(item.logo));
                    // let decode = new Buffer(item.logo, 'base64').toString()
                    console.log('********url类型的文件*******');
                    // console.log(item.logo);
                    // console.log(item.logo.toString('ascii'));
                    // console.log(`${item.logo}`);
                    // item.logo.toString()
                    // console.log(Buffer.isBuffer(item.logo));
                    // item.logo = item.logo.toString()
                    // console.log(item.logo);
                    // console.log(decode);
                    
                })
                resp.send({
                    code: 0,
                    data: {
                        status: 200,
                        result
                    }                   
                })
                console.log('成功获取用户信息', data);
                console.log('********获取logo****');
                
                
            } else {
                resp.send({
                    status: 500,
                    msg : '暂无数据'
                })
            }
        })
    })
    // console.log(sqlElec);

    

}

/***
 * 添加用户信息
 */
exports.addUserInfo = (req, resp) => {
    console.log('添加用户信息');

    let phonenumber = req.body.phonenumber
    // 设置token 是不能重复的
    let jwt = require('jsonwebtoken')
    // 用户信息  
    let payload = {number:phonenumber}
    // 口令
    let secret = 'zhimakaimen'
    // 生成token
    let token = jwt.sign(payload,secret)


    // let jwt = require('jsonwebtoken')
    // // 用户信息
    // let payload = { tel: '哈哈哈' }
    // // 口令
    // let secret = '噶阿哥'
    // // 生成token
    // let token = jwt.sign(payload,secret)
    const sqlAdd = 'insert into user (username,password,phonenumber,idnumber,address,identity,token) values (?,?,?,?,?,?,"'+token+'")'
    let adddate = [req.body.username,  req.body.password, phonenumber,req.body.idnumber, req.body.address,req.body.identity]
    console.log(adddate);
    console.log(req.body);
    console.log('*****进入');
    // console.log(req.body.username);
    console.log('jja');
    db(sqlAdd, adddate, (err, data) => {
        console.log('*****进入*******');
        if (err) {
            console.log(err);
        }
        resp.send({
            code: 0,
            data: {
                message:'添加成功'
            }
        })
    })
}

/**
 * 修改用户
 */
exports.updateUser = (req, resp) => {
    console.log('************进入修改界面**********');
    // let editUser = [req.body.username,  req.body.password, req.body.phonenumber, req.body.idnumber, req.body.address]
    // console.log(adddate);
    console.log('获取body');
    let { row } = req.body
    let username = row.username
    let password = row.password
    let phonenumber = row.phonenumber
    let address = row.address
    let idnumber = row.idnumber
   
    // console.log(req.body);
    let sqlUpdate = "update user set username=?,password=?,phonenumber=?,address=?,idnumber=? where id=?"
    console.log('*********修');
    
    db(sqlUpdate, [username, password, phonenumber, address, idnumber], (err, data) => {
        console.log('进入db');
        if (err) {
            console.log(err);
        }
        resp.send({
            code: 0,
            msg: '获取信息成功',
            data
        })
    })
}

/***
 * 删除用户
 */
exports.deleteUser = (req, resp) => {
    console.log('***********进入删除信息********');  
    let {id} = req.body
    let sqlDel = 'delete from user where id=?'
    console.log('dd');
    let dateDel = id
    db(sqlDel,dateDel, (err, res) => {
        if (err) {
            console.log('错误',err);
        }
        resp.send({
            code: 0,
            message:"删除成功"
        })
    })
}

/***
 * 查询 ，根据手机号进行查询
 */
exports.searchUser = (req, resp) => {
    console.log('******* 进入查询 *******');
    console.log(req.body);
    let phonenumber = req.body.phonenumber
    let identify = req.body.identify
    // console.log(identify);
    let phoneSql = `select * from user where phonenumber = '${phonenumber}' and identity = '${identify}'`
    db(phoneSql,(err,data) => {
        console.log('********进入*******');
        console.log(err);
        console.log(data);
        if (data.length > 0) {
            resp.send({
                code: 1,
                data: {
                    message:'手机号已注册'
                }
            })
        } else {
            resp.send({
                code: 0,
                data: {
                    msg:"该手机号未注册"
                }
            })
        }
    })
}

/***
 * 获取订单信息
 */
exports.getOrderInfo = (req, resp) => {
    console.log('**********获取订单信息**********');
    // const page = req.query.page || 1   
    // console.log(req);
     let { username, password, idnumber, address, phonenumber } = req.body
     console.log('拿到数据',req.body);
     resp.send({
        state:'success',
        data:req.body.userInfo
    })
    //  let sqlOrder = 'select * from orderinfo'
    //  console.log('11');
    //  db(sqlOrder, null,data => {
    //      console.log(data);
    //     console.log('11');
    //      let len = data.length;
    //      console.log('11');
    //      console.log(len);
         
    //     // const sql = 'select * from user by id desc limit 8 offset' + (page - 1) * 8
    //      db(sqlOrder, null, result => {
    //          console.log('result', result);
    //          result.forEach(item => {
                 
    //              item.orderdate = dayjs(item.orderdate).format('YYYY-MM-DD HH:mm:ss')
    //              console.log(item.orderdate);
    //          })
    //         console.log('result************************');
    //         // console.log(result);
    //         if (result.length > 0) {
    //             resp.send({
    //                 code: 0,
    //                 data: {
    //                     status: 200,
    //                     result
    //                 }                   
    //             })
    //             console.log('成功获取订单信息',data);
    //         } else {
    //             resp.send({
    //                 status: 500,
    //                 msg : '暂无数据'
    //             })
    //         }
    //     })
    // })
}